<?php

namespace SLN0001\Service;

use Home\DAO\OrgDAO;
use Home\Service\BizlogService;
use Home\Service\PSIBaseExService;
use SLN0001\DAO\VatDAO;

/**
 * 增值税发票Service
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class VatService extends PSIBaseExService
{
  private $LOG_CATEGORY_IN = "增值税发票进项管理";

  /**
   * 返回所有的公司列表
   *
   * @return array
   */
  public function companyList($fid)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "loginUserId" => $this->getLoginUserId(),
      "fid" => $fid
    ];

    $dao = new OrgDAO($this->db());
    return $dao->getCompanyExList($params);
  }

  /**
   * 增值税进项发票列表
   */
  public function incomeList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VatDAO($this->db());
    return $dao->incomeList($params);
  }

  /**
   * 新建或编辑进项发票记录
   */
  public function editVatIncome($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["loginUserId"] = $this->getLoginUserId();
    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();

    $db->startTrans();

    $id = $params["id"];

    $dao = new VatDAO($db);

    if ($id) {
      // 编辑
      $rc = $dao->updateIncomeBill($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新建
      $rc = $dao->addIncomeBill($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }

      $id = $params["id"];
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    return $this->ok($id);
  }

  /**
   * 删除进项发票记录
   */
  public function deleteVatIncome($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();

    $dao = new VatDAO($db);

    $rc = $dao->deleteVatIncome($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    return $this->ok();
  }

  /**
   * 查询进项发票记录的详情
   */
  public function vatIncomeInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VatDAO($this->db());
    return $dao->vatIncomeInfo($params);
  }

  /**
   * 上传进项电子发票
   */
  public function uploadIncomePDF($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    // 检查上传文件是否是pdf以及大小
    $fileInfo = $_FILES["vat_income_file"];
    $error = $fileInfo["error"];
    if ($error > 0) {
      return $this->bad("上传文件错误，错误码 = {$error}");
    }
    $fileType = $fileInfo["type"];

    if ($fileType != "application/pdf") {
      return $this->bad("请上传PDF文件");
    }
    $fileSize = $fileInfo["size"] / (1024 * 1024);
    if ($fileSize > 5) {
      return $this->bad("上传PDF文件不能超过5M");
    }

    $params["uploadFilePath"] = $fileInfo["tmp_name"];
    $params["fileSize"] = $fileInfo["size"];

    $db = $this->db();

    $db->startTrans();

    $dao = new VatDAO($db);

    $rc = $dao->uploadIncomePDF($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    return $this->ok();
  }

  /**
   * 查看进项电子发票
   */
  public function downloadIncomePDF($params)
  {
    if ($this->isNotOnline()) {
      die("用户未登录");
    }

    $dao = new VatDAO($this->db());
    $result = $dao->getVatIncomeFile($params);
    if (!$result["success"]) {
      return die($result["msg"]);
    }

    $log = $result["log"];
    // 记录业务日志
    $bs = new BizlogService($this->db());
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $fileData = $result["fileData"];
    $fileSize = $result["fileSize"];
    $ref = $result["ref"];
    $fileChineseName = "增值税发票_{$ref}.pdf";

    //设置头信息
    //声明浏览器输出的是pdf
    header("Content-Type: application/pdf");
    //声明浏览器返回大小是按字节进行计算
    header("Accept-Ranges:bytes");
    //告诉浏览器文件的总大小
    header("Content-Length:" . $fileSize);
    //声明下载文件的名称
    $cn = urlencode($fileChineseName);
    header("Content-Disposition:inline;filename={$cn}");
    echo $fileData;
  }

  /**
   * 获得采购入库单主表列表
   */
  public function pwbillList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $dao = new VatDAO($this->db());
    return $dao->pwbillList($params);
  }

  /**
   * 获得采购入库单商品明细记录列表
   */
  public function pwBillDetailList($pwbillId)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "id" => $pwbillId,
      "companyId" => $this->getCompanyId(),
    ];

    $dao = new VatDAO($this->db());
    return $dao->pwBillDetailList($params);
  }

  /**
   * 采购退货出库单列表
   */
  public function prbillList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params["loginUserId"] = $this->getLoginUserId();

    $dao = new VatDAO($this->db());
    return $dao->prbillList($params);
  }

  /**
   * 采购退货出库单明细列表
   */
  public function prBillDetailList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params["companyId"] = $this->getCompanyId();

    $dao = new VatDAO($this->db());
    return $dao->prBillDetailList($params);
  }

  /**
   * 新建关联
   */
  public function addRelation($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();

    $dao = new VatDAO($db);

    $rc = $dao->addRelation($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    $id = $params["id"];

    return $this->ok($id);
  }

  /**
   * 查询发票和单据关联列表
   */
  public function incomeBillList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new VatDAO($this->db());
    return $dao->incomeBillList($params);
  }

  /**
   * 删除关联
   */
  public function deleteRelation($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();

    $dao = new VatDAO($db);

    $rc = $dao->deleteRelation($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    return $this->ok();
  }

  /**
   * 核销
   */
  public function commitVatIncome($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();

    $dao = new VatDAO($db);

    $params["loginUserId"] = $this->getLoginUserId();
    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $rc = $dao->commitVatIncome($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY_IN);

    $db->commit();

    return $this->ok();
  }
}
